package com.daisuxia.allin;

import java.util.Map;
import java.util.TreeMap;

import com.daisuxia.back.utils.SysCacheUtils;


public class SybPayService {
	/**
	 * 
	 * @param trxamt 金额
	 * @param reqsn 订单号
	 * @param paytype 交易方式(W01：微信扫码支付,A01：支付宝扫码支付)
	 * @param body 订单标题
	 * @param remark 备注
	 * @param validtime 有效时间
	 * @param notify_url 回调地址
	 * @return
	 * @throws Exception
	 */
	public static Map<String,String> pay(long trxamt,String reqsn,String paytype,String body,String remark,String validtime) throws Exception{
		Map<String,String> allinContants = SysCacheUtils.getConfigParams("ALLIN");
		HttpConnectionUtil http = new HttpConnectionUtil(allinContants.get("APIURL_ALLIN")+"/pay");
		http.init();
		String currentCust = allinContants.get("CURRENT_CUST_ALLIN");
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", allinContants.get("CUSTID_ALLIN_"+currentCust));
		params.put("appid", allinContants.get("APPID_ALLIN_"+currentCust));
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));
		params.put("reqsn", reqsn);
		params.put("paytype", paytype);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("body", body);
		params.put("remark", remark);
		params.put("validtime", validtime);
		params.put("notify_url", allinContants.get("NOTIFYURL_ALLIN"));
		params.put("sign", SybUtil.sign(params,allinContants.get("APPKEY_ALLIN_"+currentCust)));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
		
	}
	
	public Map<String,String> cancel(long trxamt,String reqsn,String oldtrxid,String oldreqsn) throws Exception{
		Map<String,String> allinContants = SysCacheUtils.getConfigParams("ALLIN");
		HttpConnectionUtil http = new HttpConnectionUtil(allinContants.get("APIURL_ALLIN")+"/cancel");
		http.init();
		String currentCust = allinContants.get("CURRENT_CUST_ALLIN");
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", allinContants.get("CUSTID_ALLIN_"+currentCust));
		params.put("appid", allinContants.get("APPID_ALLIN_"+currentCust));
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));
		params.put("reqsn", reqsn);
		params.put("oldtrxid", oldtrxid);
		params.put("oldreqsn", oldreqsn);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,allinContants.get("APPKEY_ALLIN_"+currentCust)));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	
	public Map<String,String> refund(long trxamt,String reqsn,String oldtrxid,String oldreqsn) throws Exception{
		Map<String,String> allinContants = SysCacheUtils.getConfigParams("ALLIN");
		HttpConnectionUtil http = new HttpConnectionUtil(allinContants.get("APIURL_ALLIN")+"/refund");
		http.init();
		String currentCust = allinContants.get("CURRENT_CUST_ALLIN");
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", allinContants.get("CUSTID_ALLIN_"+currentCust));
		params.put("appid", allinContants.get("APPID_ALLIN_"+currentCust));
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));
		params.put("reqsn", reqsn);
		params.put("oldreqsn", oldreqsn);
		params.put("oldtrxid", oldtrxid);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,allinContants.get("APPKEY_ALLIN_"+currentCust)));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	
	public Map<String,String> query(String reqsn,String trxid) throws Exception{
		Map<String,String> allinContants = SysCacheUtils.getConfigParams("ALLIN");
		HttpConnectionUtil http = new HttpConnectionUtil(allinContants.get("APIURL_ALLIN")+"/query");
		http.init();
		String currentCust = allinContants.get("CURRENT_CUST_ALLIN");
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", allinContants.get("CUSTID_ALLIN_"+currentCust));
		params.put("appid", allinContants.get("APPID_ALLIN_"+currentCust));
		params.put("version", "11");
		params.put("reqsn", reqsn);
		params.put("trxid", trxid);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,allinContants.get("APPKEY_ALLIN_"+currentCust)));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map<String,String> handleResult(String result) throws Exception{
		Map<String,String> allinContants = SysCacheUtils.getConfigParams("ALLIN");
		String currentCust = allinContants.get("CURRENT_CUST_ALLIN");
		Map map = SybUtil.json2Obj(result, Map.class);
		if(map == null){
			throw new Exception("返回数据错误");
		}
		if("SUCCESS".equals(map.get("retcode"))){
			TreeMap tmap = new TreeMap();
			tmap.putAll(map);
			String sign = tmap.remove("sign").toString();
			String sign1 = SybUtil.sign(tmap,allinContants.get("APPKEY_ALLIN_"+currentCust));
			if(sign1.toLowerCase().equals(sign.toLowerCase())){
				return map;
			}else{
				throw new Exception("验证签名失败");
			}
			
		}else{
			throw new Exception(map.get("retmsg").toString());
		}
	}
	
	
}
